<?php

namespace App\Http\Controllers\Activity\Merchant;

use App\Http\Controllers\BaseController;
use App\Service\Activity\ShopActivityService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;

class ShopActivityController extends BaseController
{

    /**
     * 霸屏通用接口
     * @param Request $request
     * @return JsonResponse
     */
    public function dominatingScreen(Request $request): JsonResponse
    {
        $shopId = (int)$request->header('shopId', 0);

        $data = ShopActivityService::getDominatingScreen($shopId);
        return $this->success($data);

    }

    /**
     * 霸屏活动信息显示
     * @param Request $request
     * @return JsonResponse
     */
    public function getActivityInfo(Request $request): JsonResponse
    {
        $shopId     = (int)$request->header('shopId', 0);
        $activityId = (int)$request->get('activityId', 0);

        $this->validate(
            $request, [
                'activityId' => 'required|integer',
            ]
        );

        $detailInfo = ShopActivityService::getActivityInfo($activityId, $shopId);
        if ($detailInfo['code']) {
            return $this->success($detailInfo['data']);
        } else {
            return $this->fail($detailInfo['code'], $detailInfo['msg']);
        }
    }


    /**
     * 活动-创建门店参与记录
     * @param Request $request
     * @return JsonResponse
     */
    public function createOrder(Request $request): JsonResponse
    {
        $shopId           = (int)$request->header('shopId', 0);
        $activityActionId = (int)$request->get('activityActionId', 0);

        $this->validate(
            $request, [
                'activityActionId' => 'required|integer',
            ]
        );

        $userId   = getRmsUserId();
        $username = getRmsUserInfoByAuth()->username;

        return $this->success(ShopActivityService::createOrder($shopId, $userId, $username, $activityActionId));
    }


    /**
     * 活动-取消订单
     * @param Request $request
     * @return JsonResponse
     */
    public function cancelOrder(Request $request): JsonResponse
    {
        $shopId               = (int)$request->header('shopId', 0);
        $activityActionJoinId = (int)$request->get('activityActionJoinId', 0);

        $this->validate(
            $request, [
                'activityActionJoinId' => 'required|integer',
            ]
        );

        $userId   = getRmsUserId();
        $username = getRmsUserInfoByAuth()->username;

        return $this->success(ShopActivityService::cancelOrder($shopId, $userId, $username, $activityActionJoinId));
    }


    /**
     * 表单新增记录
     * @param Request $request
     * @return JsonResponse
     */
    public function inputsAdd(Request $request): JsonResponse
    {
        $shopId           = (int)$request->header('shopId', 0);
        $activityActionId = (int)$request->get('activityActionId', 0);

        $this->validate(
            $request, [
                'activityActionId' => 'required|integer',
            ]
        );

        $userId   = getRmsUserId();
        $username = getRmsUserInfoByAuth()->username;

        return $this->success(ShopActivityService::inputsAdd($shopId, $userId, $username, $activityActionId));
    }

    /**
     * 表单记录详情
     * @param Request $request
     * @return array|JsonResponse
     */
    public function inputsInfo(Request $request)
    {
        $shopId   = (int)$request->header('shopId', 0);
        $cruiseId = (int)$request->get('cruiseId', 0);

        $this->validate(
            $request, [
                'cruiseId' => 'required|integer',
            ]
        );

        $userId = getRmsUserId();

        return $this->success(ShopActivityService::inputsInfo($shopId, $userId, $cruiseId));
    }


    /**
     * 表单记录修改
     * @param Request $request
     * @return JsonResponse
     */
    public function inputsUpdate(Request $request): JsonResponse
    {
        $this->validate(
            $request, [
                'field'      => 'required|string',
                'value'      => 'required',
                'auditValue' => 'array',
                'cruiseId'   => 'required|integer',
            ]
        );

        $shopId     = (int)$request->header('shopId', 0);
        $cruiseId   = (int)$request->get('cruiseId', 0);
        $field      = (string)$request->get('field');
        $value      = $request->get('value');
        $auditValue = (array)$request->get('auditValue');
        $userId     = getRmsUserId();

        ShopActivityService::inputsUpdate($shopId, $userId, $cruiseId, $field, $value, $auditValue);
        return $this->success();
    }

    /**
     * 表单记录提交
     * @param Request $request
     * @return JsonResponse
     */
    public function inputsSubmit(Request $request): JsonResponse
    {
        $shopId   = (int)$request->header('shopId', 0);
        $cruiseId = (int)$request->get('cruiseId', 0);

        $this->validate(
            $request, [
                'cruiseId' => 'required|integer',
            ]
        );
        $userId = getRmsUserId();

        ShopActivityService::inputsSubmit($shopId, $userId, $cruiseId);
        return $this->success();
    }

    /**
     * 反馈记录详情
     * @param Request $request
     * @return JsonResponse
     * @throws ValidationException
     */
    public function feedbackDetail(Request $request): JsonResponse
    {
        $activityId = (int)$request->get('activityId', 0);
        $shopId     = (int)$request->header('shopId', 0);

        $this->validate(
            $request, [
                'activityId' => 'required|integer',
            ]
        );

        return $this->success(ShopActivityService::getFeedbackDetail($activityId, $shopId, $request->user()));
    }

    /**
     * 反馈记录详情保存
     * @param Request $request
     * @return JsonResponse
     * @throws ValidationException
     */
    public function feedbackSave(Request $request): JsonResponse
    {
        $activityId     = (int)$request->get('activityId', 0);
        $shopId         = (int)$request->header('shopId', 0);
        $feedbackImages = (array)$request->get('feedbackImages', []);
        $feedbackVideos = (array)$request->get('feedbackVideos', []);
        $feedback       = (string)$request->get('feedback', '');
        $shopContact    = (string)$request->get('shopContact', '');

        $this->validate(
            $request, [
                'activityId'     => 'required|integer',
                'shopContact'    => 'required|string',
                'feedbackImages' => 'required_without:feedbackVideos|array',
                'feedbackVideos' => 'required_without:feedbackImages|array'
            ]
        );

        $userId   = getRmsUserId();
        $username = getRmsUserInfoByAuth()->username;

        ShopActivityService::feedbackSave(
            $activityId, $shopId, $userId, $username,
            [
                'feedbackImages' => $feedbackImages,
                'feedbackVideos' => $feedbackVideos,
                'feedback'       => $feedback,
                'shopContact'    => $shopContact
            ]
        );

        return $this->success();
    }
}
